package com.zimmur.app.mocolink.manage.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zimmur.app.mocolink.domain.form.RefundExpress;
import com.zimmur.app.mocolink.domain.model.Order;
import com.zimmur.app.mocolink.domain.model.RefundOrder;
import com.zimmur.app.mocolink.domain.param.OrderParamForManage;
import com.zimmur.app.mocolink.domain.param.RefundParam;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.OrderInfo;
import com.zimmur.app.mocolink.domain.vo.RefundOrderInfo;
import com.zimmur.app.mocolink.domain.vo.RefundOrderVo;
import com.zimmur.app.mocolink.domain.vo.RefundOrderVoForManage;
import com.zimmur.app.mocolink.manage.web.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.manage.web.common.StatusCodeEnum;
import com.zimmur.app.mocolink.service.IOrderService;
import com.zimmur.app.mocolink.service.IRefundOrderService;

@Controller
@RequestMapping("/refund")
public class RefundController extends BaseController {
	@Autowired
	private IRefundOrderService refundOrderService;
	@Autowired
	private IOrderService orderService;
	
	@RequestMapping("/index")
	public String index(ModelMap model){
		Integer pageNo = 1;
		RefundParam param = new RefundParam(pageNo);
		PageUtil<RefundOrderInfo> page = refundOrderService.selectByParam(param);
		model.addAttribute("page", page);
		model.addAttribute("param", param);
		return "center/refund/list";
	}
	@RequestMapping("/query")
	public String query(ModelMap model){
		Integer pageNo = getParam("pageNo", 1);
		String status = getParam("status");
		String keyword = getParam("keyword");
		String createTimeStart = getParam("createTimeStart");
		String createTimeEnd = getParam("createTimeEnd");
		String refundTimeStart = getParam("refundTimeStart");
		String refundTimeEnd = getParam("refundTimeEnd");
		Double refundJeMin = getParam("refundJeMin", 0D);
		Double refundJeMax = getParam("refundJeMax", 0D);
		RefundParam param = new RefundParam(pageNo, status, keyword, createTimeStart, createTimeEnd, refundTimeStart, refundTimeEnd, refundJeMin, refundJeMax);
		PageUtil<RefundOrderInfo> page = refundOrderService.selectByParam(param);
		model.addAttribute("page", page);
		model.addAttribute("param", param);
		return "center/refund/list";
	}
	@RequestMapping("/showDetail")
	public String showDetail(ModelMap model){
		Integer refundId=getParam("refundId",0);
		RefundOrderVoForManage refundOrderVo = refundOrderService.selectDetailById(refundId);
		model.addAttribute("refundOrderVo", refundOrderVo);
		return "center/refund/detail";
	}
	@RequestMapping("/refundExpress")
	public String refundExpress(ModelMap model){
		Integer refundId=getParam("refundId",0);
		model.addAttribute("refundId", refundId);
		return "center/refund/express";
	}
	@RequestMapping("/takeGoods")
	public String takeGoods(ModelMap model){
		Integer refundId=getParam("refundId",0);
		RefundOrderVoForManage refundOrderVo = refundOrderService.selectDetailById(refundId);
		model.addAttribute("refundOrderVo", refundOrderVo);
		return "center/refund/takeGoods";
	}
	@RequestMapping("/refundMoney")
	public String refundMoney(ModelMap model){
		Integer refundId=getParam("refundId",0);
		RefundOrder refundOrder = refundOrderService.selectById(refundId);
		double totalJe = 0;
		double applyJe =0;
		if(refundOrder!=null){
			Order order = orderService.selectByOrderId(refundOrder.getOrderId());
			if(order!=null){
				double payJe = order.getPayJe()==null?0:order.getPayJe().doubleValue();
				double refundJe = order.getRefundJe()==null?0:order.getRefundJe().doubleValue();
				totalJe = payJe-refundJe;
				applyJe = refundOrder.getApplyJe()==null?0:refundOrder.getApplyJe().doubleValue();
				
				model.addAttribute("refundNo", refundOrder.getRefundNo());
				model.addAttribute("refundId", refundOrder.getId());
			}
		}
		model.addAttribute("totalJe", totalJe);
		model.addAttribute("applyJe", applyJe);
		return "center/refund/refundMoney";
	}
	
	@RequestMapping("/edit")
	public String edit(ModelMap model){
		Integer refundId=getParam("refundId",0);
		RefundOrderVoForManage refundOrderVo = refundOrderService.selectDetailById(refundId);
		model.addAttribute("refundOrderVo", refundOrderVo);
		return "center/refund/edit";
	}
	
	@ResponseBody
	@RequestMapping("/checkRefund")
	public ReturnJsonEntity checkRefund(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer refundId = getParam("refundId", 0);
		Integer status = getParam("status", 0);
		String username = getUsername();
		int flag = refundOrderService.checkRefund(refundId, status, username);
		if(flag>0){
			codeEnum=StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/saveExpress")
	public ReturnJsonEntity saveExpress(RefundExpress refundExpress){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		if(StringUtils.isBlank(refundExpress.getShipCompany(),refundExpress.getShipNo(),refundExpress.getShipDate())){
			codeEnum=StatusCodeEnum.CODE100001;
		}else{
			int flag = refundOrderService.saveRefundExpress(refundExpress);
			if(flag>0){
				codeEnum=StatusCodeEnum.CODE000000;
			}else if(flag == -1){
				codeEnum=StatusCodeEnum.CODE420401;
			}
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/saveSignInfo")
	public ReturnJsonEntity saveSignInfo(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer refundId = getParam("refundId", 0);
		String refundInfos = getParam("refundInfos");
		int flag = refundOrderService.saveRefundGoods(refundId, refundInfos);
		if(flag>0){
			codeEnum=StatusCodeEnum.CODE000000;
		}else if(flag == -1){
			codeEnum=StatusCodeEnum.CODE420401;
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/saveRefundMoney")
	public ReturnJsonEntity saveRefundMoney(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer refundId = getParam("refundId", 0);
		Double money = getParam("money",0D);
		String username=getUsername();
		if(money<=0){
			codeEnum=StatusCodeEnum.CODE420402;
		}else{
			int flag = refundOrderService.refundMoney(refundId, money, username);
			if(flag>0){
				codeEnum=StatusCodeEnum.CODE000000;
			}else if(flag == -1){
				codeEnum=StatusCodeEnum.CODE420401;
			}else if(flag == -2){
				codeEnum=StatusCodeEnum.CODE420404;
			}else if(flag == -3){
				codeEnum=StatusCodeEnum.CODE420403;
			}
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/saveRefund")
	public ReturnJsonEntity saveRefund(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer refundId = getParam("refundId", 0);
		String remark=getParam("remark");
		String deteleIds = getParam("delIds");
		String detailInfos=getParam("detailInfos");
		String username=getUsername();
		if(StringUtils.isBlank(detailInfos) || refundId<=0){
			codeEnum=StatusCodeEnum.CODE100001;
		}else{
			int flag = refundOrderService.saveRefund(refundId, remark, deteleIds, detailInfos, username);
			if(flag>0){
				codeEnum=StatusCodeEnum.CODE000000;
			}else if(flag == -1){
				codeEnum=StatusCodeEnum.CODE420401;
			}else if(flag == -2){
				codeEnum=StatusCodeEnum.CODE420405;
			}else if(flag == -3){
				codeEnum=StatusCodeEnum.CODE420406;
			}
		}
		entity.init(codeEnum);
		return entity;
	}
}
